{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Week04_数据挖掘笔记"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Part_01\n",
    "1. 代码小试："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#准备模块\n",
    "import pandas as pd\n",
    "from requests_html import HTMLSession"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#首先抓取首页的单一页面\n",
    "\n",
    "url = \"https://www.liepin.com/zhaopin/?keyword=产品经理\"\n",
    "session = HTMLSession()\n",
    "r = session.get( url )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<Element 'a' href='/zhaopin/?init=-1&headckid=bfafdb2c1d981244&fromSearchBtn=2&keyword=PRD&ckid=bfafdb2c1d981244°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=0dffe979ff7a05797e4400fc8b1f45e9&d_curPage=0&d_pageSize=40&d_headId=0dffe979ff7a05797e4400fc8b1f45e9&curPage=1'>, <Element 'a' href='/zhaopin/?init=-1&headckid=bfafdb2c1d981244&fromSearchBtn=2&keyword=PRD&ckid=bfafdb2c1d981244°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=0dffe979ff7a05797e4400fc8b1f45e9&d_curPage=0&d_pageSize=40&d_headId=0dffe979ff7a05797e4400fc8b1f45e9&curPage=2'>, <Element 'a' href='/zhaopin/?init=-1&headckid=bfafdb2c1d981244&fromSearchBtn=2&keyword=PRD&ckid=bfafdb2c1d981244°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=0dffe979ff7a05797e4400fc8b1f45e9&d_curPage=0&d_pageSize=40&d_headId=0dffe979ff7a05797e4400fc8b1f45e9&curPage=3'>, <Element 'a' href='/zhaopin/?init=-1&headckid=bfafdb2c1d981244&fromSearchBtn=2&keyword=PRD&ckid=bfafdb2c1d981244°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=0dffe979ff7a05797e4400fc8b1f45e9&d_curPage=0&d_pageSize=40&d_headId=0dffe979ff7a05797e4400fc8b1f45e9&curPage=4'>, <Element 'a' href='/zhaopin/?init=-1&headckid=bfafdb2c1d981244&fromSearchBtn=2&keyword=PRD&ckid=bfafdb2c1d981244°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=0dffe979ff7a05797e4400fc8b1f45e9&d_curPage=0&d_pageSize=40&d_headId=0dffe979ff7a05797e4400fc8b1f45e9&curPage=1'>, <Element 'a' class=('last',) href='/zhaopin/?init=-1&headckid=bfafdb2c1d981244&fromSearchBtn=2&keyword=PRD&ckid=bfafdb2c1d981244°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=0dffe979ff7a05797e4400fc8b1f45e9&d_curPage=0&d_pageSize=40&d_headId=0dffe979ff7a05797e4400fc8b1f45e9&curPage=9' title='末页'>]\n"
     ]
    }
   ],
   "source": [
    "#第一步：\n",
    "xpath_翻页a = '//div[@class=\"pagerbar\"]/a'  #抓取翻页内容标签：class=pagebar后面的所有a元素的内容\n",
    "\n",
    "#第二步：\n",
    "#为了减少不需要的参数，以下代码作用为：\n",
    "# satrt-with 只取 href 这个参数后面以 /zhaopin 开头的内容，其他的过滤掉\n",
    "xpath_翻页a = '//div[@class=\"pagerbar\"]/a[starts-with(@href,\"/zhaopin\")]'\n",
    "\n",
    "print (r.html.xpath(xpath_翻页a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'2': '/zhaopin/?init=-1&headckid=bfafdb2c1d981244&fromSearchBtn=2&keyword=PRD&ckid=bfafdb2c1d981244°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=0dffe979ff7a05797e4400fc8b1f45e9&d_curPage=0&d_pageSize=40&d_headId=0dffe979ff7a05797e4400fc8b1f45e9&curPage=1', '3': '/zhaopin/?init=-1&headckid=bfafdb2c1d981244&fromSearchBtn=2&keyword=PRD&ckid=bfafdb2c1d981244°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=0dffe979ff7a05797e4400fc8b1f45e9&d_curPage=0&d_pageSize=40&d_headId=0dffe979ff7a05797e4400fc8b1f45e9&curPage=2', '4': '/zhaopin/?init=-1&headckid=bfafdb2c1d981244&fromSearchBtn=2&keyword=PRD&ckid=bfafdb2c1d981244°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=0dffe979ff7a05797e4400fc8b1f45e9&d_curPage=0&d_pageSize=40&d_headId=0dffe979ff7a05797e4400fc8b1f45e9&curPage=3', '5': '/zhaopin/?init=-1&headckid=bfafdb2c1d981244&fromSearchBtn=2&keyword=PRD&ckid=bfafdb2c1d981244°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=0dffe979ff7a05797e4400fc8b1f45e9&d_curPage=0&d_pageSize=40&d_headId=0dffe979ff7a05797e4400fc8b1f45e9&curPage=4', '下一页': '/zhaopin/?init=-1&headckid=bfafdb2c1d981244&fromSearchBtn=2&keyword=PRD&ckid=bfafdb2c1d981244°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=0dffe979ff7a05797e4400fc8b1f45e9&d_curPage=0&d_pageSize=40&d_headId=0dffe979ff7a05797e4400fc8b1f45e9&curPage=1', '': '/zhaopin/?init=-1&headckid=bfafdb2c1d981244&fromSearchBtn=2&keyword=PRD&ckid=bfafdb2c1d981244°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=0dffe979ff7a05797e4400fc8b1f45e9&d_curPage=0&d_pageSize=40&d_headId=0dffe979ff7a05797e4400fc8b1f45e9&curPage=9'}\n"
     ]
    }
   ],
   "source": [
    "#第三步：\n",
    "#不懂这几行代码，但我知道它是列表推导，作为我们的参数钥匙，留以待用\n",
    "\n",
    "href_列表 = [x.xpath('//@href')[0] for x in r.html.xpath(xpath_翻页a)]\n",
    "#print (href_列表)\n",
    "\n",
    "文字_列表 = [x.text for x in r.html.xpath(xpath_翻页a)]\n",
    "#print (文字_列表)\n",
    "\n",
    "href_字典 = {x.text:x.xpath('//@href')[0]  for x in r.html.xpath(xpath_翻页a)}\n",
    "print (href_字典)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Part_02\n",
    "1. 构建参数模板"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>scheme</th>\n",
       "      <th>netloc</th>\n",
       "      <th>path</th>\n",
       "      <th>params</th>\n",
       "      <th>query</th>\n",
       "      <th>fragment</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>/zhaopin/</td>\n",
       "      <td></td>\n",
       "      <td>init=-1&amp;headckid=bfafdb2c1d981244&amp;fromSearchBt...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>/zhaopin/</td>\n",
       "      <td></td>\n",
       "      <td>init=-1&amp;headckid=bfafdb2c1d981244&amp;fromSearchBt...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>/zhaopin/</td>\n",
       "      <td></td>\n",
       "      <td>init=-1&amp;headckid=bfafdb2c1d981244&amp;fromSearchBt...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>/zhaopin/</td>\n",
       "      <td></td>\n",
       "      <td>init=-1&amp;headckid=bfafdb2c1d981244&amp;fromSearchBt...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>/zhaopin/</td>\n",
       "      <td></td>\n",
       "      <td>init=-1&amp;headckid=bfafdb2c1d981244&amp;fromSearchBt...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>/zhaopin/</td>\n",
       "      <td></td>\n",
       "      <td>init=-1&amp;headckid=bfafdb2c1d981244&amp;fromSearchBt...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  scheme netloc       path params  \\\n",
       "0                /zhaopin/          \n",
       "1                /zhaopin/          \n",
       "2                /zhaopin/          \n",
       "3                /zhaopin/          \n",
       "4                /zhaopin/          \n",
       "5                /zhaopin/          \n",
       "\n",
       "                                               query fragment  \n",
       "0  init=-1&headckid=bfafdb2c1d981244&fromSearchBt...           \n",
       "1  init=-1&headckid=bfafdb2c1d981244&fromSearchBt...           \n",
       "2  init=-1&headckid=bfafdb2c1d981244&fromSearchBt...           \n",
       "3  init=-1&headckid=bfafdb2c1d981244&fromSearchBt...           \n",
       "4  init=-1&headckid=bfafdb2c1d981244&fromSearchBt...           \n",
       "5  init=-1&headckid=bfafdb2c1d981244&fromSearchBt...           "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "scheme      1\n",
      "netloc      1\n",
      "path        1\n",
      "params      1\n",
      "query       5\n",
      "fragment    1\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ckid</th>\n",
       "      <th>curPage</th>\n",
       "      <th>d_ckId</th>\n",
       "      <th>d_curPage</th>\n",
       "      <th>d_headId</th>\n",
       "      <th>d_pageSize</th>\n",
       "      <th>d_sfrom</th>\n",
       "      <th>fromSearchBtn</th>\n",
       "      <th>headckid</th>\n",
       "      <th>init</th>\n",
       "      <th>keyword</th>\n",
       "      <th>siTag</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>bfafdb2c1d981244°radeFlag=0</td>\n",
       "      <td>1</td>\n",
       "      <td>0dffe979ff7a05797e4400fc8b1f45e9</td>\n",
       "      <td>0</td>\n",
       "      <td>0dffe979ff7a05797e4400fc8b1f45e9</td>\n",
       "      <td>40</td>\n",
       "      <td>search_unknown</td>\n",
       "      <td>2</td>\n",
       "      <td>bfafdb2c1d981244</td>\n",
       "      <td>-1</td>\n",
       "      <td>PRD</td>\n",
       "      <td>1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>bfafdb2c1d981244°radeFlag=0</td>\n",
       "      <td>2</td>\n",
       "      <td>0dffe979ff7a05797e4400fc8b1f45e9</td>\n",
       "      <td>0</td>\n",
       "      <td>0dffe979ff7a05797e4400fc8b1f45e9</td>\n",
       "      <td>40</td>\n",
       "      <td>search_unknown</td>\n",
       "      <td>2</td>\n",
       "      <td>bfafdb2c1d981244</td>\n",
       "      <td>-1</td>\n",
       "      <td>PRD</td>\n",
       "      <td>1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>bfafdb2c1d981244°radeFlag=0</td>\n",
       "      <td>3</td>\n",
       "      <td>0dffe979ff7a05797e4400fc8b1f45e9</td>\n",
       "      <td>0</td>\n",
       "      <td>0dffe979ff7a05797e4400fc8b1f45e9</td>\n",
       "      <td>40</td>\n",
       "      <td>search_unknown</td>\n",
       "      <td>2</td>\n",
       "      <td>bfafdb2c1d981244</td>\n",
       "      <td>-1</td>\n",
       "      <td>PRD</td>\n",
       "      <td>1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>bfafdb2c1d981244°radeFlag=0</td>\n",
       "      <td>4</td>\n",
       "      <td>0dffe979ff7a05797e4400fc8b1f45e9</td>\n",
       "      <td>0</td>\n",
       "      <td>0dffe979ff7a05797e4400fc8b1f45e9</td>\n",
       "      <td>40</td>\n",
       "      <td>search_unknown</td>\n",
       "      <td>2</td>\n",
       "      <td>bfafdb2c1d981244</td>\n",
       "      <td>-1</td>\n",
       "      <td>PRD</td>\n",
       "      <td>1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>bfafdb2c1d981244°radeFlag=0</td>\n",
       "      <td>1</td>\n",
       "      <td>0dffe979ff7a05797e4400fc8b1f45e9</td>\n",
       "      <td>0</td>\n",
       "      <td>0dffe979ff7a05797e4400fc8b1f45e9</td>\n",
       "      <td>40</td>\n",
       "      <td>search_unknown</td>\n",
       "      <td>2</td>\n",
       "      <td>bfafdb2c1d981244</td>\n",
       "      <td>-1</td>\n",
       "      <td>PRD</td>\n",
       "      <td>1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>bfafdb2c1d981244°radeFlag=0</td>\n",
       "      <td>9</td>\n",
       "      <td>0dffe979ff7a05797e4400fc8b1f45e9</td>\n",
       "      <td>0</td>\n",
       "      <td>0dffe979ff7a05797e4400fc8b1f45e9</td>\n",
       "      <td>40</td>\n",
       "      <td>search_unknown</td>\n",
       "      <td>2</td>\n",
       "      <td>bfafdb2c1d981244</td>\n",
       "      <td>-1</td>\n",
       "      <td>PRD</td>\n",
       "      <td>1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                          ckid curPage                            d_ckId  \\\n",
       "0  bfafdb2c1d981244°radeFlag=0       1  0dffe979ff7a05797e4400fc8b1f45e9   \n",
       "1  bfafdb2c1d981244°radeFlag=0       2  0dffe979ff7a05797e4400fc8b1f45e9   \n",
       "2  bfafdb2c1d981244°radeFlag=0       3  0dffe979ff7a05797e4400fc8b1f45e9   \n",
       "3  bfafdb2c1d981244°radeFlag=0       4  0dffe979ff7a05797e4400fc8b1f45e9   \n",
       "4  bfafdb2c1d981244°radeFlag=0       1  0dffe979ff7a05797e4400fc8b1f45e9   \n",
       "5  bfafdb2c1d981244°radeFlag=0       9  0dffe979ff7a05797e4400fc8b1f45e9   \n",
       "\n",
       "  d_curPage                          d_headId d_pageSize         d_sfrom  \\\n",
       "0         0  0dffe979ff7a05797e4400fc8b1f45e9         40  search_unknown   \n",
       "1         0  0dffe979ff7a05797e4400fc8b1f45e9         40  search_unknown   \n",
       "2         0  0dffe979ff7a05797e4400fc8b1f45e9         40  search_unknown   \n",
       "3         0  0dffe979ff7a05797e4400fc8b1f45e9         40  search_unknown   \n",
       "4         0  0dffe979ff7a05797e4400fc8b1f45e9         40  search_unknown   \n",
       "5         0  0dffe979ff7a05797e4400fc8b1f45e9         40  search_unknown   \n",
       "\n",
       "  fromSearchBtn          headckid init keyword  \\\n",
       "0             2  bfafdb2c1d981244   -1     PRD   \n",
       "1             2  bfafdb2c1d981244   -1     PRD   \n",
       "2             2  bfafdb2c1d981244   -1     PRD   \n",
       "3             2  bfafdb2c1d981244   -1     PRD   \n",
       "4             2  bfafdb2c1d981244   -1     PRD   \n",
       "5             2  bfafdb2c1d981244   -1     PRD   \n",
       "\n",
       "                                           siTag  \n",
       "0  1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw  \n",
       "1  1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw  \n",
       "2  1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw  \n",
       "3  1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw  \n",
       "4  1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw  \n",
       "5  1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ckid             1\n",
      "curPage          5\n",
      "d_ckId           1\n",
      "d_curPage        1\n",
      "d_headId         1\n",
      "d_pageSize       1\n",
      "d_sfrom          1\n",
      "fromSearchBtn    1\n",
      "headckid         1\n",
      "init             1\n",
      "keyword          1\n",
      "siTag            1\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# A-2 建构参数模板：找到关键参数及参数结构\n",
    "\n",
    "# 需要模组库\n",
    "from urllib.parse import urlparse, parse_qs\n",
    "import pandas as pd\n",
    "from IPython.display import display, HTML\n",
    "\n",
    "# 总体目标：输入 href_列表, 建构出参数字典\n",
    "\n",
    "# urlparse 解析后丢入数据框\n",
    "df = pd.DataFrame([ urlparse(x) for x in href_列表])\n",
    "df_qs = pd.DataFrame([{k:v[0] for k,v in parse_qs(x).items()} for x in df['query'] ])\n",
    "\n",
    "display(df)\n",
    "print(df.nunique())\n",
    "display(df_qs)\n",
    "print(df_qs.nunique())\n",
    "\n",
    "df_qs.curPage\n",
    "df_qs = df_qs.assign (curPage_int=df_qs.curPage.astype(int)) # 变成整数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 自我总结以上构件模板代码：\n",
    "  + 首先需要引入模板组件库\n",
    "  + 用pandas处理好数据存入数据框里面\n",
    "  + 观察：然后找出不同，那个不同的东西就是我们要找的钥匙"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Part_03\n",
    "1. 构建参数模板：curPage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'init': ['-1'], 'headckid': ['bfafdb2c1d981244'], 'fromSearchBtn': ['2'], 'keyword': ['PRD'], 'ckid': ['bfafdb2c1d981244°radeFlag=0'], 'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'], 'd_sfrom': ['search_unknown'], 'd_ckId': ['0dffe979ff7a05797e4400fc8b1f45e9'], 'd_curPage': ['0'], 'd_pageSize': ['40'], 'd_headId': ['0dffe979ff7a05797e4400fc8b1f45e9'], 'curPage': ['1']}\n",
      "{'2': '/zhaopin/?init=-1&headckid=bfafdb2c1d981244&fromSearchBtn=2&keyword=PRD&ckid=bfafdb2c1d981244°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=0dffe979ff7a05797e4400fc8b1f45e9&d_curPage=0&d_pageSize=40&d_headId=0dffe979ff7a05797e4400fc8b1f45e9&curPage=1', '3': '/zhaopin/?init=-1&headckid=bfafdb2c1d981244&fromSearchBtn=2&keyword=PRD&ckid=bfafdb2c1d981244°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=0dffe979ff7a05797e4400fc8b1f45e9&d_curPage=0&d_pageSize=40&d_headId=0dffe979ff7a05797e4400fc8b1f45e9&curPage=2', '4': '/zhaopin/?init=-1&headckid=bfafdb2c1d981244&fromSearchBtn=2&keyword=PRD&ckid=bfafdb2c1d981244°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=0dffe979ff7a05797e4400fc8b1f45e9&d_curPage=0&d_pageSize=40&d_headId=0dffe979ff7a05797e4400fc8b1f45e9&curPage=3', '5': '/zhaopin/?init=-1&headckid=bfafdb2c1d981244&fromSearchBtn=2&keyword=PRD&ckid=bfafdb2c1d981244°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=0dffe979ff7a05797e4400fc8b1f45e9&d_curPage=0&d_pageSize=40&d_headId=0dffe979ff7a05797e4400fc8b1f45e9&curPage=4', '下一页': '/zhaopin/?init=-1&headckid=bfafdb2c1d981244&fromSearchBtn=2&keyword=PRD&ckid=bfafdb2c1d981244°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=0dffe979ff7a05797e4400fc8b1f45e9&d_curPage=0&d_pageSize=40&d_headId=0dffe979ff7a05797e4400fc8b1f45e9&curPage=1', '': '/zhaopin/?init=-1&headckid=bfafdb2c1d981244&fromSearchBtn=2&keyword=PRD&ckid=bfafdb2c1d981244°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=0dffe979ff7a05797e4400fc8b1f45e9&d_curPage=0&d_pageSize=40&d_headId=0dffe979ff7a05797e4400fc8b1f45e9&curPage=9'}\n"
     ]
    }
   ],
   "source": [
    "# A-2 建构参数模板：找到关键参数及参数结构\n",
    "\n",
    "def parse_url_qs_for_curPage (url):\n",
    "    six_parts = urlparse(url) \n",
    "    out = parse_qs(six_parts.query)\n",
    "    return (out)\n",
    "\n",
    "# 取一例做模板\n",
    "参数模板 = parse_url_qs_for_curPage(href_列表[0])\n",
    "print (参数模板)\n",
    "\n",
    "print (href_字典)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. 这部分应该是廖老师自己构建了一个函数，将需要用到的参数转化成为字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "9\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{0: {'init': ['-1'],\n",
       "  'headckid': ['bfafdb2c1d981244'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['用户体验'],\n",
       "  'ckid': ['bfafdb2c1d981244°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['0dffe979ff7a05797e4400fc8b1f45e9'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['0dffe979ff7a05797e4400fc8b1f45e9'],\n",
       "  'curPage': [0]},\n",
       " 1: {'init': ['-1'],\n",
       "  'headckid': ['bfafdb2c1d981244'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['用户体验'],\n",
       "  'ckid': ['bfafdb2c1d981244°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['0dffe979ff7a05797e4400fc8b1f45e9'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['0dffe979ff7a05797e4400fc8b1f45e9'],\n",
       "  'curPage': [1]},\n",
       " 2: {'init': ['-1'],\n",
       "  'headckid': ['bfafdb2c1d981244'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['用户体验'],\n",
       "  'ckid': ['bfafdb2c1d981244°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['0dffe979ff7a05797e4400fc8b1f45e9'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['0dffe979ff7a05797e4400fc8b1f45e9'],\n",
       "  'curPage': [2]},\n",
       " 3: {'init': ['-1'],\n",
       "  'headckid': ['bfafdb2c1d981244'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['用户体验'],\n",
       "  'ckid': ['bfafdb2c1d981244°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['0dffe979ff7a05797e4400fc8b1f45e9'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['0dffe979ff7a05797e4400fc8b1f45e9'],\n",
       "  'curPage': [3]},\n",
       " 4: {'init': ['-1'],\n",
       "  'headckid': ['bfafdb2c1d981244'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['用户体验'],\n",
       "  'ckid': ['bfafdb2c1d981244°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['0dffe979ff7a05797e4400fc8b1f45e9'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['0dffe979ff7a05797e4400fc8b1f45e9'],\n",
       "  'curPage': [4]},\n",
       " 5: {'init': ['-1'],\n",
       "  'headckid': ['bfafdb2c1d981244'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['用户体验'],\n",
       "  'ckid': ['bfafdb2c1d981244°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['0dffe979ff7a05797e4400fc8b1f45e9'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['0dffe979ff7a05797e4400fc8b1f45e9'],\n",
       "  'curPage': [5]},\n",
       " 6: {'init': ['-1'],\n",
       "  'headckid': ['bfafdb2c1d981244'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['用户体验'],\n",
       "  'ckid': ['bfafdb2c1d981244°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['0dffe979ff7a05797e4400fc8b1f45e9'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['0dffe979ff7a05797e4400fc8b1f45e9'],\n",
       "  'curPage': [6]},\n",
       " 7: {'init': ['-1'],\n",
       "  'headckid': ['bfafdb2c1d981244'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['用户体验'],\n",
       "  'ckid': ['bfafdb2c1d981244°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['0dffe979ff7a05797e4400fc8b1f45e9'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['0dffe979ff7a05797e4400fc8b1f45e9'],\n",
       "  'curPage': [7]},\n",
       " 8: {'init': ['-1'],\n",
       "  'headckid': ['bfafdb2c1d981244'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['用户体验'],\n",
       "  'ckid': ['bfafdb2c1d981244°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['0dffe979ff7a05797e4400fc8b1f45e9'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['0dffe979ff7a05797e4400fc8b1f45e9'],\n",
       "  'curPage': [8]},\n",
       " 9: {'init': ['-1'],\n",
       "  'headckid': ['bfafdb2c1d981244'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['用户体验'],\n",
       "  'ckid': ['bfafdb2c1d981244°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['0dffe979ff7a05797e4400fc8b1f45e9'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['0dffe979ff7a05797e4400fc8b1f45e9'],\n",
       "  'curPage': [9]}}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A-3 建构参数模板生成器：keyword curPage\n",
    "def 参数模板生成(keyword, curPage):\n",
    "    参数 = 参数模板.copy()\n",
    "    参数['curPage'] = curPage\n",
    "    参数['keyword'] = keyword\n",
    "    return (参数)\n",
    "\n",
    "参数_keyword_用户体验_curPage = { \n",
    "    i:参数模板生成(curPage = [i], \\\n",
    "                  keyword = ['用户体验']) \\\n",
    "    for i,v in href_字典.items()\\\n",
    "    }\n",
    "\n",
    "# print(参数_keyword_用户体验_curPage) # 只生成本页有的额外翻页URL, 并没有推估到&curPage=9,也没有这页\n",
    "\n",
    "print (df_qs.curPage_int.min()) # 最小值只有1\n",
    "print (df_qs.curPage_int.max()) # 最大值只有9\n",
    "\n",
    "# 应该是 0 (本页)....9(最大值)\n",
    "\n",
    "参数_keyword_用户体验_curPage = { \n",
    "    i:参数模板生成(curPage = [i], \\\n",
    "                  keyword = ['用户体验']) \\\n",
    "    for i in range(0,df_qs.curPage_int.max()+1)\\\n",
    "    }\n",
    "参数_keyword_用户体验_curPage"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4. 到这里为止，我是还未完全看懂代码的用意，但视频中廖老师说这里已经抓到我们的“万能钥匙”了！  \n",
    "虽然没看懂，也是很开心！！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 爬虫礼仪\n",
    "  + time.sleep"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 3.99 s\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "from random import random\n",
    "\n",
    "%%time\n",
    "time.sleep(3+4*random())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'requests_liepin' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<timed exec>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'requests_liepin' is not defined"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "url = \"https://www.liepin.com/zhaopin/\"\n",
    "\n",
    "list_df = list()\n",
    "for k,v in 参数_keyword_用户体验_curPage.items():\n",
    "    payload = v\n",
    "    df = requests_liepin( url, params = payload)\n",
    "    time.sleep(3+4*random())  #放慢脚步 3-7秒, 平均约5秒\n",
    "    df = df.assign (curPage = k)  # 区分  curPage\n",
    "    list_df.append(df)\n",
    "\n",
    "df_all = pd.concat(list_df).reset_index()\n",
    "df_all.index.name = '序'\n",
    "\n",
    "# 上周C-4   输出\n",
    "df_all.to_excel(\"20春_Web数据挖掘_week04_liepin_翻页.xlsx\",\\\n",
    "                sheet_name=\"用户体验\")\n",
    "\n",
    "# 预估时间: 5秒*10 =50\n",
    "# 预估数量: 40*10 =400"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5. 以上则是本周暂时所练习的内容笔记，还没有完全弄懂和弄全，今后还会继续研究下去！\n",
    "重点总结：\n",
    "  + 构建参数模板，找到关键参数\n",
    "  + 分析url，解析成字典内容\n",
    "  + 制作万能钥匙\n",
    "  + 爬虫礼仪"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
